package com.group15.phms;

import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddVitalSignsActivity extends Activity{
	String currentUser;
	SharedPreferences logins;
	DatabaseHelper db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//show vital signs form
		setContentView(R.layout.activity_add_vital_signs);
		// Show the Up button in the action bar.
		setupActionBar();
		
		Button btn = (Button) findViewById(R.id.ButtonSubmitVitals);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            	//get data to create new vital sign object
            	submitVitalSigns(v);
            }
        });//end onClick
	}
	
	//Set up the {@link android.app.ActionBar}.
	private void setupActionBar() { getActionBar().setDisplayHomeAsUpEnabled(true); }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_vital_signs, menu);
		return true;
	}
	
	Intent intent;
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		case R.id.action_logout:
			intent = new Intent(this, Login.class);
	    	startActivity(intent);
	    	finish();
			return true;
		case R.id.action_search:
			intent = new Intent(this, Search.class);
	    	startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	public void submitVitalSigns(View button){
		
		final EditText editCholesterol = (EditText) findViewById(R.id.EditCholesterol);
		int cholesterol = Integer.parseInt( editCholesterol.getText().toString() );

		final EditText editBloodGlucose = (EditText) findViewById(R.id.EditBloodGlucose);
		int glucose = Integer.parseInt( editBloodGlucose.getText().toString() );

		final EditText editSysBP = (EditText) findViewById(R.id.EditSysBloodPressure);
		int sysBP = Integer.parseInt( editSysBP.getText().toString() );
		
		final EditText editDiaBP = (EditText) findViewById(R.id.EditDiaBloodPressure);
		int diaBP = Integer.parseInt( editDiaBP.getText().toString() );
				
		Calendar vDate = Calendar.getInstance();
		final CustomDatePicker editDate = (CustomDatePicker) findViewById(R.id.datePicker1);
		vDate.set( editDate.getYear(), editDate.getMonth(), editDate.getDayOfMonth() );
		System.out.printf("%d %d %d", cholesterol, glucose, sysBP);
		System.out.printf("%d %d %d", editDate.getYear(), editDate.getMonth(), editDate.getDayOfMonth());
		
		//VitalSigns v = new VitalSigns(cholesterol, glucose, sysBP, diaBP, weight, vDate);
		logins = getSharedPreferences("logins",MODE_PRIVATE);
		currentUser = logins.getString("currentUser","INVALID");
		db= new DatabaseHelper(getApplicationContext());
		
		boolean result = VitalSignsController.addVitalSigns(cholesterol, glucose, sysBP, diaBP, vDate.getTime(), 
				getApplicationContext(), currentUser, db);

		if(result == false){ //vital signs not added
			Toast.makeText(AddVitalSignsActivity.this, "There was an error adding your vital signs.", 
					Toast.LENGTH_LONG).show();
		}
		else{ //vital signs added
			Toast.makeText(AddVitalSignsActivity.this, "Vital signs added!", 
					Toast.LENGTH_LONG).show();
			Intent changeIntent = new Intent(AddVitalSignsActivity.this, VitalSignsActivity.class);
			startActivity(changeIntent);
		}
	}
	

}
